Skip to main content

Server interceptors

gRPC server interceptors intercept incoming RPC requests. What comes in, to the server.

Interceptor methods to override for server:

  • UnaryServerHandler: Intercepts a unary RPC.
  • ClientStreamingServerHandler: Intercepts a client-streaming RPC.
  • ServerStreamingServerHandler: Intercepts a server-streaming RPC.
  • DuplexStreamingServerHandler: Intercepts a bidirectional-streaming RPC.

The signature is rather similar, but the method names follows the *Handler naming structure, suggesting that the specific method will 'handle' an incoming call.

Configure interceptors on servers

Configure on all services

Title
 services.AddGrpc(options =>
{
options.Interceptors.Add<ServerLoggerInterceptor>();
});

Configure on a specific service

Title
       .AddGrpc()
.AddServiceOptions<GreeterService>(options =>
{
options.Interceptors.Add<ServerLoggerInterceptor>();
});

When it comes to the execution, server interceptors are executed in the order they are added to the InterceptorCollection. First the global ones are executed, and then those configured for a single service(in the order they are added).